package Physics;

import java.util.Vector;

/*
 import java.util.Iterator;
 public class ForceRegistry {
 class ForceRegistration
 {
 public RigidBody object;
 public ForceGenerator fg;
 }
 private Vector m_Registry = new Vector();

 private m_Registry forceGenerators;

 public void add(RigidBody p_obj, ForceGenerator p_fg)
 {
 ForceRegistration r;
 r.object = p_obj;
 r.fg = p_fg;
 boolean found = false;
 Iterator i = m_Registry.iterator();
 i = forceGenerators.begin();
 while(i.hasNext())
 {
 if (i.object == object && i.fg == fg)
 {
 found = true;
 break;
 }
 }

 if (!found)
 forceGenerators.push_back(r);
 }
 public void remove(RigidBody p_obj)
 {
 Iterator i = m_Registry.iterator();
 i = forceGenerators.begin();
 while(i.hasNext())
 {
 if (i.object == object)
 {
 i = forceGenerators.erase(i);
 }
 }
 }
 void updateForces(float dt)
 {
 Iterator i = m_Registry.iterator();
 i = forceGenerators.begin();
 while(i.hasNext())
 {
 i.fg.updateForce(i.object, dt);
 }
 }
 }
 */

///////////////////////////////////////////
//GlobalForceRegistry
//
//Applies the given ForceGenerator to ALL objects
//provided by a RigidBody vector
///////////////////////////////////////////
public class GlobalForceRegistry {
	
	public GlobalForceRegistry()
	{
		m_objectList = new Vector<CollisionPrimitive>();
		m_forceGenerators = new Vector<ForceGenerator>();
	}
	
	Vector<CollisionPrimitive> m_objectList; // the list of objects to have the
												// force generator applied to

	// for tidier code
	Vector<ForceGenerator> m_forceGenerators;

	// STL vector object that keeps track of all force generators
	// and their respective objects

	// adds a FG to the list
	public void add(ForceGenerator fg) {
		boolean found = false;
		for (int i = 0; i < m_forceGenerators.size(); ++i) {
			if (m_forceGenerators.get(i) == fg) {
				found = true;
				break;
			}
		}

		if (!found)
			m_forceGenerators.add(fg);
	}

	// removes a FG from the list
	public void remove(ForceGenerator fg) {
		for (int i = 0; i < m_forceGenerators.size(); ++i) {
			if (m_forceGenerators.get(i) == fg)
				m_forceGenerators.remove(i);
		}
	}

	// sets the list of object
	public void setObjectList(Vector<CollisionPrimitive> objects) {
		m_objectList = objects;
	}

	// removes all FGs
	public void clear() {
	}

	// tells all force generators to update their corresponding objects
	public void updateForces(double dt) {
		// loop through all FGs telling them to update their objects
		for (int i = 0; i < m_forceGenerators.size(); ++i) {
			ForceGenerator fg = m_forceGenerators.get(i);
			for (int j = 0; j < m_objectList.size(); ++j) {
				RigidBody body = m_objectList.get(j).m_body;
				if (body != null) {
					if (body.m_activated) {
						fg.updateForce(body, dt);
					}
				}
			}
		}
	}

}